package easy.trace.agent.support;

import easy.trace.agent.Sampler;

import java.util.concurrent.atomic.AtomicLong;

public class SampleImp implements Sampler {
    private AtomicLong count = new AtomicLong();
    private int baseNumber = 100;
    private Long lastTime = -1L;

    public SampleImp() {
        this.lastTime = System.currentTimeMillis();
    }

    @Override
    public boolean isSample() {
        boolean isSample = true;
        long n = this.count.incrementAndGet();
        if (System.currentTimeMillis() - this.lastTime.longValue() < 1000L) {
            if (n > (long)this.baseNumber) {
                n %= 10L;
                if (n != 0L) {
                    isSample = false;
                }
            }
        } else {
            this.count.getAndSet(0L);
            this.lastTime = System.currentTimeMillis();
        }

        return isSample;
    }
}
